package com.example.lcode.greedy;
import java.util.Arrays;

public class Question435 {

    public int eraseOverlapIntervals(int[][] intervals) {
        int res = 0;
        if (intervals == null || intervals.length == 0) {
            return res;
        }
        Arrays.sort(intervals, (o1, o2) -> {
            if (o1[0] > o2[0]) {
                return 1;
            } else if (o1[0] == o2[0]) {
                return 0;
            }
            return -1;
        });

        int max = intervals[0][1];
        int min = intervals[0][0];
        for (int i = 1; i < intervals.length; i++) {
            if(intervals[i][0] < max) {
                if (intervals[i][1] < max) {
                    min = intervals[i][0];
                    max = intervals[i][1];
                }
                res ++;
            } else {
                max = intervals[i][1];
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[][] arrs = {{0,2},{1,3},{2,4},{3,5},{4,6}};
        Question435 question435 = new Question435();
        int i = question435.eraseOverlapIntervals(arrs);
        System.out.println(i);
    }
}
